// lottery/pages/detail/detail.js
var common = require("../../../common/common.js").common;

var app = getApp();

var util = require("../../../utils/util.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        prizes: [ {
            fonts: [ {
                text: "0",
                top: "10%",
                fontColor: "#CA682C",
                fontSize: "24px"
            } ],
            background: "#f9be93",
            imgs: [ {
                src: "https://tuangou121.oss-cn-hangzhou.aliyuncs.com/image/202307/28/img_168973012517.jpeg",
                top: "44%",
                width: "40px",
                height: "40px"
            } ]
        }, {
            fonts: [ {
                text: "1",
                top: "10%",
                fontColor: "#FFFFFF",
                fontSize: "24px"
            } ],
            background: " #f25151"
        }, {
            fonts: [ {
                text: "2",
                top: "10%",
                fontColor: "#CA682C",
                fontSize: "24px"
            } ],
            background: "#f9be93"
        }, {
            fonts: [ {
                text: "3",
                top: "10%",
                fontColor: "#FFFFFF",
                fontSize: "24px"
            } ],
            background: " #f25151"
        }, {
            fonts: [ {
                text: "4",
                top: "10%",
                fontColor: "#CA682C",
                fontSize: "24px"
            } ],
            background: "#f9be93"
        }, {
            fonts: [ {
                text: "5",
                top: "10%",
                fontColor: "#FFFFFF",
                fontSize: "24px"
            } ],
            background: " #f25151"
        } ],
        buttons: [ {
            radius: "50px",
            background: "transparent"
        }, {
            radius: "45px",
            background: "transparent"
        }, {
            radius: "40px",
            background: "transparent",
            fonts: [ {
                text: "",
                top: "-20px"
            } ]
        }, {
            imgs: [ {
                src: `${app.globalData.domain.slice(0, -10)}/img/pointer1.png`,
                width: "112px",
                height: "112px",
                top: "-56px"
            } ]
        } ],
        id: 0,
        myPrizeList: [],
        products: [],
        details: {},
        color1: "",
        color2: "",
        prizeTitle: "",
        prizeAlert: false,
        prizeText: "",
        btnStatus: true
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10),
            id: options.id
        });
        this.getTotalData();
        this.getData();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage() {},
    getData() {
        var that = this;
        common.request({
            endpoint: "Lottery/manage/active-read",
            method: "post",
            data: {
                id: that.data.id
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    myPrizeList: res.extra.my_prize,
                    products: res.extra.products,
                    details: res.extra
                });
                var ww = res.extra.background_colour.split(",");
                let arr = res.extra.products;
                let arr2 = [];
                var bg1 = "";
                var bg2 = "";
                var fcolor1 = "";
                var fcolor2 = "";
                if (res.extra.style == 1) {
                    bg1 = "#f9be93";
                    bg2 = "#f25151";
                    fcolor1 = "#CA682C";
                    fcolor2 = "#fff";
                } else if (res.extra.style == 2) {
                    bg1 = "#6f25d2";
                    bg2 = "#a969ff";
                    fcolor1 = "#fff";
                    fcolor2 = "#fff";
                } else if (res.extra.style == 3) {
                    bg1 = "#ffb9ac";
                    bg2 = "#ffe7e3";
                    fcolor1 = "#F14E47";
                    fcolor2 = "#F24F48";
                } else if (res.extra.style == 4) {
                    bg1 = "#ffb9b7";
                    bg2 = "#fedfdd";
                    fcolor1 = "#C84A37";
                    fcolor2 = "#C84A36";
                } else if (res.extra.style == 5) {
                    bg1 = "#e9e0ff";
                    bg2 = "#7021e8";
                    fcolor1 = "#7021E8";
                    fcolor2 = "#FFFFFF";
                } else if (res.extra.style == 6) {
                    bg1 = "#ddf3e0";
                    bg2 = "#f6fff0";
                    fcolor1 = "#428D70";
                    fcolor2 = "#428D70";
                }
                for (var i = 0; i < arr.length; i++) {
                    if (i % 2 == 0) {
                        let data = {
                            fonts: [ {
                                text: arr[i].title,
                                top: "10%",
                                fontColor: fcolor1,
                                fontSize: "14px",
                                lineClamp: 2,
                                lengthLimit: "50%"
                            } ],
                            background: bg1,
                            imgs: [ {
                                src: arr[i].img_url,
                                top: "44%",
                                width: "40px",
                                height: "40px"
                            } ]
                        };
                        arr2.push(data);
                    } else {
                        let data = {
                            fonts: [ {
                                text: arr[i].title,
                                top: "10%",
                                fontColor: fcolor2,
                                fontSize: "14px",
                                lineClamp: 2,
                                lengthLimit: "50%"
                            } ],
                            background: bg2,
                            imgs: [ {
                                src: arr[i].img_url,
                                top: "44%",
                                width: "40px",
                                height: "40px"
                            } ]
                        };
                        arr2.push(data);
                    }
                }
                that.setData({
                    prizes: arr2,
                    color1: ww[0],
                    color2: ww[1]
                });
            }
        });
    },
    start() {
        if (!this.data.btnStatus) {
            return false;
        }
        wx.showModal({
            title: "提示",
            content: `是否确认消耗${this.data.details.integral}积分参与抽奖？`,
            complete: res => {
                if (res.cancel) {}
                if (res.confirm) {
                    this.setData({
                        btnStatus: false
                    });
                    // 获取抽奖组件实例
                                        const child = this.selectComponent("#myLucky");
                    // 调用play方法开始旋转
                    // 用定时器模拟请求接口
                                        let index = "";
                    this.raffle().then(res => {
                        for (var i = 0; i < this.data.products.length; i++) {
                            if (res == 0) {
                                index = 0;
                                this.setData({
                                    prizeTitle: "",
                                    prizeText: "谢谢惠顾"
                                });
                            }
                            if (res == this.data.products[i].id) {
                                index = i;
                                this.setData({
                                    prizeTitle: this.data.products[index].title,
                                    prizeText: "恭喜抽中"
                                });
                            }
                            child.lucky.play();
                            setTimeout(() => {
                                // 3s 后得到中奖索引 (假设抽到第0个奖品)
                                // 调用stop方法然后缓慢停止
                                child.lucky.stop(index);
                            }, 3e3);
                        }
                    });
                }
            }
        });
    },
    raffle() {
        var that = this;
        return new Promise((resolve, reject) => {
            common.request({
                endpoint: "Lottery/manage/raffle",
                method: "post",
                data: {
                    id: that.data.id
                },
                header: {
                    "content-type": "application/json"
                },
                success: res => {
                    if (res.code == 0) {
                        resolve(res.extra.prize_id);
                    } else {
                        this.setData({
                            btnStatus: true
                        });
                        wx.showModal({
                            title: "提示",
                            content: res.message,
                            complete: res => {
                                if (res.cancel) {}
                                if (res.confirm) {}
                            }
                        });
                    }
                },
                fail: err => {
                    this.setData({
                        btnStatus: true
                    });
                    wx.showModal({
                        title: "提示",
                        content: err.message,
                        complete: res => {
                            if (res.cancel) {}
                            if (res.confirm) {}
                        }
                    });
                }
            });
        });
    },
    end(event) {
        // 中奖奖品详情
        this.setData({
            prizeAlert: true
        });
        this.getData();
        this.getTotalData();
    },
    toPrize(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/lottery/pages/prize/prize?id=" + this.data.id
        });
    },
    closeAlert() {
        this.setData({
            prizeAlert: false,
            btnStatus: true
        });
    },
    back() {
        wx.navigateBack(-1);
    },
    getTotalData() {
        var that = this;
        common.request({
            endpoint: "My/myinfo/my_tab_info",
            method: "post",
            success(res) {
                let totalData = res.extra;
                totalData.balance = util.numberFormatter(totalData.balance, 2);
                totalData.point = util.numberFormatter(totalData.point, 2);
                totalData.spec_fund = util.numberFormatter(totalData.spec_fund, 2);
                that.setData({
                    totalData: totalData
                });
            }
        });
    }
});